package springboot.test3.demo.service.Impl;

import springboot.test3.demo.mapper.MongoUserDao;
import springboot.test3.demo.entity.User;
import springboot.test3.demo.param.DeleteUserParam;
import springboot.test3.demo.param.FindUserParam;
import springboot.test3.demo.service.Mongoservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author ：Dongdequan
 * @date ：Created in 2022/4/1 14:26
 * @modified By：
 */
@Service
public class MongoServiceImpl implements Mongoservice {

    @Autowired
    private MongoUserDao mongoDao;

    public Optional<User> findUser(FindUserParam param) {
        Optional<User> users = mongoDao.findById(param.getId());
        return users;
    }

    public List<User> findAll() {
        List<User> users = mongoDao.findAll();
        return users;
    }

    @Override
    public String updateUser(User param) {
        if (mongoDao.findById(param.getId()).isPresent()) {
            mongoDao.save(param);
            return "success";
        } else {
            return "fail";
        }
    }

    @Override
    public String insertUser(User param) {
        User user = mongoDao.insert(param);
        return user.getId();
    }

    @Override
    public String deleteUser(DeleteUserParam param) {
        mongoDao.deleteById(param.getId());
        return "success";
    }
}
